package com.example.flashsales.controller;


import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.ZhekouDto;
import com.example.flashsales.entity.vo.ZhekouVo;
import com.example.flashsales.role.role;
import com.example.flashsales.service.impl.ZhekouServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 卷的控制类
 */
@RequestMapping("/volume")
@Controller
public class VolumeController {

    @Autowired
    ZhekouServiceImpl zheKouService;

    /**
     * 创建卷
     * @param httpServletRequest
     * @param zhekouDto 状态 0未使用 1已使用 2已过期 3未发布
     * @return
     */
    @ResponseBody
    @role(role = 46)
    @PostMapping("/createVolume")
    public Result createVolume(HttpServletRequest httpServletRequest, @RequestBody ZhekouDto zhekouDto){
        if (zhekouDto == null  || zhekouDto.getCreateTime() == null ||  zhekouDto.getEndTime() == null){
            return Result.error("请输入参数");
        }
        if (zhekouDto.getMoney() < 0){
            return Result.error("触发金额不能小于0");
        }
        if ( zhekouDto.getZhekou() > 1 || zhekouDto.getZhekou() < 0){
            return Result.error("折扣不能小于0或大于1");
        }
        if (zhekouDto.getEndTime().getTime() < zhekouDto.getCreateTime().getTime()){
            return Result.error("开始时间不能小于结束时间");
        }
        if (zhekouDto.getCount() <= 0){
            return Result.error("创建的票数不能小于0");
        }
        if (zhekouDto.getName() == null || zhekouDto.getName().equals("")){
            return Result.error("名字不能为空");
        }
        zhekouDto.setState(3);
        if (zheKouService.createVolume(zhekouDto)) {
            return Result.ok("创建成功");
        }
        return Result.error("创建失败");
    }


    /**
     * 查看卷
     * @param httpServletRequest
     * @param state 状态 0未使用 1已使用 2已过期 3未发布 4 不能使用
     * @param name 卷的名称
     * @param create_time 创建时间
     * @param end_time 发布时间
     * @return
     */
    @ResponseBody
    @GetMapping("/viewVolume")
    @role(role = 47)
    public Result viewVolume(HttpServletRequest httpServletRequest, Integer state, String name, Date create_time,Date end_time,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer num,Long id,Long orderId,Long userId){
        if (state != null && state != 0 && state != 1 && state != 2 && state != 3){
            return Result.error("状态不存在");
        }
        if (page <= 0 || num <= 0){
            return Result.error("页数或条数不能小于等于0");
        }
        PageHelper.startPage(page,num);
        List<ZhekouVo> zhekouVos = zheKouService.viewVolume(state,name,create_time,end_time,page,num,id,orderId,userId);
        return Result.ok("查看成功").setData(new PageInfo<>(zhekouVos));
    }


    /**
     * 禁用卷
     * @param httpServletRequest
     * @param list
     * @return
     */
    @ResponseBody
    @PostMapping("/disableVolume")
    @role(role = 48)
    public Result disableVolume(HttpServletRequest httpServletRequest,@RequestBody List<Long> list){
        if (list == null || list.isEmpty()){
            return Result.error("请输入参数");
        }
        List<Long> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            final List<ZhekouVo> zhekouVos = zheKouService.viewVolume(null, null, null, null, null, null, list.get(i), null, null);
            if (zhekouVos == null || zhekouVos.isEmpty()){
                return Result.error("修改失败").setData("这张卷不存在->"+list.get(i));
            }
            if (zhekouVos.get(0).getState().equals("已使用") || zhekouVos.get(0).getState().equals("不能使用")){
                list1.add(list.get(i));
            }
        }
        if (!list1.isEmpty()){
            return Result.error("修改失败，这些卷已经被使用或者不能被使用").setData(list1);
        }
        if (zheKouService.disableVolume(list)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 发布卷
     * @param httpServletRequest
     * @param list
     * @return
     */
    @ResponseBody
    @PostMapping("/releaseVolume")
    @role(role = 49)
    public Result releaseVolume(HttpServletRequest httpServletRequest,@RequestBody List<Long> list){
        if (list == null || list.isEmpty()){
            return Result.error("请输入参数");
        }
        List<Long> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            final List<ZhekouVo> zhekouVos = zheKouService.viewVolume(null, null, null, null, null, null, list.get(i), null, null);
            if (zhekouVos == null || zhekouVos.isEmpty()){
                return Result.error("发布失败").setData("这张卷不存在->"+list.get(i));
            }
            if (!zhekouVos.get(0).getState().equals("未发布")){
                list1.add(list.get(i));
            }
        }
        if (!list1.isEmpty()){
            return Result.error("修改失败，这些卷不是为发布状态").setData(list1);
        }
        if (zheKouService.releaseVolume(list)) {
            return Result.ok("发布成功");
        }
        return Result.error("发布失败");
    }
}
